/**
 * Copyright (C)  Gadglet .
 *
 * This file is part of Gadglet
 *
 * Gadglet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Gadglet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Gadglet. If not, see <http://www.gnu.org/licenses/>.
 */

package com.gadglet.util;

import javax.servlet.http.HttpServletRequest;

import com.gadglet.params.SharedConstants;


public class UrlUtils {

	/*
	 * There are 2 king of url:
	 *  1 - anangadgets.appspot.com/gadgets/<gadgetName>/gadgetName.xml
	 *  2 - anangadgets.appspot.com/gadgets/<gadgetName>/<gadgetFileName>
	 * 
	 */
	
	public static String getGadgetHome(String gadgetName) 
	{
		String url =null;
		
		if(gadgetName!=null)
			url = SharedConstants.gadgetDirName+gadgetName+"/";
		return url;
	}
	
	public static String getGadgetName(String url) 
	{
		
		String gadgetName = null;
		String workStr = null;
		
		if(url!= null){
			try
			{
				workStr = url.substring(SharedConstants.gadgetDirName.length());
				gadgetName = workStr.substring(0, workStr.indexOf("/"));
			}
			catch (Exception e)
			{
				// do nothing url is unknown...
			}
			
		}
			
		
		return(gadgetName);
	}
	
	public static boolean isXmlRequest (String url) {
						
		if(url!= null){
			try
			{
			if(url.indexOf(".xml")>0)
					return true;
				
			}
			catch (Exception e)
			{
				// do nothing url is unknown...
			}
			
		}
		
		return false;
	}
	
	public static String getHomeUrl(HttpServletRequest request, boolean isSecure, boolean usedUrlScheme)
	{
		StringBuffer url = new StringBuffer();
		if(request!=null)
			{
			if (usedUrlScheme)
				url.append(request.getScheme()+"://");
			else if (isSecure && request.getServerPort()!=8888)// for debug in eclipse
				url.append("https://");
			else
				url.append("http://");
			
			url.append(request.getServerName());
			// for debug in eclipse
			if(request.getServerPort()==8888)
				url.append(":"+request.getServerPort());
			return url.toString();
			} 
		return null;
	}
	public static String getHomeUrl(HttpServletRequest request)
	{
		return getHomeUrl(request, false,true);
	}
	
	public static boolean isOnDevPort8888(HttpServletRequest request){
		
		if (request.getServerPort()==8888)
			return true;
	
		return false;
	}
	
	/*
	 * There are 2 king of url:
	 *  1 - anangadgets.appspot.com/gadgets/<gadgetName>/gadgetName.xml
	 *  2 - anangadgets.appspot.com/gadgets/<gadgetName>/<gadgetFileName>
	 * 
	 */
	public static String getGadgetXMLUrl(HttpServletRequest request,String gadgetName,String gadgetFileName)
	{
		StringBuffer url = new StringBuffer();
		if(request!=null && gadgetName!=null)
		{
			url.append(getHomeUrl(request,false,false));
			url.append(SharedConstants.gadgetDirName);
			
			if(gadgetFileName != null && !gadgetFileName.equals(""))
				url.append(gadgetName+"/"+gadgetFileName);
			else
				url.append(gadgetName+"/"+gadgetName+".xml");
			
			return url.toString();
		} 
		return null;
	}
	
	
	
}
